﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Windows.Media.Imaging;
using DiscountCoupon.Phone.Common;

namespace DiscountCoupon
{
    public partial class MainPage : PhoneApplicationPage
    {
        /// <summary>
        /// 下载管理器
        /// </summary>
        protected DownloadHelper dwHelper;
        /// <summary>
        /// 缓存管理器
        /// </summary>
        protected IsolateCache cache;
        
        /// <summary>
        /// 列表缓存的key
        /// </summary>
        const string CARD_LIST_CONFIG="CardList.json";
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            this.pivot.SelectionChanged += new SelectionChangedEventHandler(pivot_SelectionChanged);
            
            dwHelper = new DownloadHelper();
            //dwHelper.onRequestException += new DownloadHelper.RequestExceptionHandler(helper_onRequestException);
            DownloadHelper.onAllRequestCompleted += new EventHandler(dwHelper_AllRequestCompleted);
            DownloadHelper.onBeginRequest += new EventHandler(DownloadHelper_onBeginRequest);
            DownloadHelper.onRequestException+=new DownloadHelper.RequestExceptionHandler(App.helper_onRequestException);

            cache = new IsolateCache();
        }
       

        /// <summary>
        /// 标签项改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //取消所有请求
            dwHelper.CancelAllRequest();
            if (this.pivot.SelectedIndex == 0)
            {
            }
            else if (this.pivot.SelectedIndex == 1)
            {
                Second_ListBox_Loaded(sender, e);
            }
            else if (this.pivot.SelectedIndex == 2)
            {
                AboutPageLoaded(sender, e);
            }
        }

        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsCardListLoaded)
            {
                First_ListBox_Loaded(sender, e);
            }           
        }

        /// <summary>
        /// 完全下载完后不显示进度条
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void dwHelper_AllRequestCompleted(object sender, EventArgs e)
        {
            this.loadingProgressBar.IsIndeterminate = false;
        }

        /// <summary>
        /// 开始请求时显示进度条
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void DownloadHelper_onBeginRequest(object sender, EventArgs e)
        {
            this.loadingProgressBar.IsIndeterminate = true;
        }
    }
}